package com.yfkj.common.message.utils;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;

import com.alibaba.fastjson.JSON;
import com.yfkj.common.message.constant.SendAccountConstant;
import com.yfkj.common.message.dto.account.WeChatMiniProgramAccount;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;



/**
 * 获取账号信息工具类
 */
@Slf4j
@Configuration
public class AccountUtils {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Bean
    public RedisTemplateWxRedisOps redisTemplateWxRedisOps() {
        return new RedisTemplateWxRedisOps(redisTemplate);
    }

    /**
     * 获取账号配置信息
     * @param accountConfig
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getAccount(String accountConfig, Class<T> clazz){

        if (clazz.equals(WxMaService.class)) {
            return (T)initMiniProgramService(JSON.parseObject(accountConfig, WeChatMiniProgramAccount.class));
        } else {
            return JSON.parseObject(accountConfig, clazz);
        }

    }

    /**
     * 通过脚本名 匹配到对应的短信账号
     *
     * @param scriptName 脚本名
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getSmsAccountByScriptName(String scriptName, Class<T> clazz) {
//        try {
//            List<ChannelAccount> channelAccountList = channelAccountDao.findAllByIsDeletedEqualsAndSendChannelEquals(CommonConstant.FALSE, ChannelType.SMS.getCode());
//            for (ChannelAccount channelAccount : channelAccountList) {
//                try {
//                    SmsAccount smsAccount = JSON.parseObject(channelAccount.getAccountConfig(), SmsAccount.class);
//                    if (smsAccount.getScriptName().equals(scriptName)) {
//                        return JSON.parseObject(channelAccount.getAccountConfig(), clazz);
//                    }
//                } catch (Exception e) {
//                    log.error("AccountUtils#getSmsAccount parse fail! e:{},account:{}", Throwables.getStackTraceAsString(e), JSON.toJSONString(channelAccount));
//                }
//            }
//        } catch (Exception e) {
//            log.error("AccountUtils#getSmsAccount fail! e:{}", Throwables.getStackTraceAsString(e));
//        }
//        log.error("AccountUtils#getSmsAccount not found!:{}", scriptName);
        return null;
    }

    /**
     * 初始化微信小程序
     * access_token 用redis存储
     *
     * @return
     */
    private WxMaService initMiniProgramService(WeChatMiniProgramAccount miniProgramAccount) {
        WxMaService wxMaService = new WxMaServiceImpl();
        WxMaRedisBetterConfigImpl config = new WxMaRedisBetterConfigImpl(redisTemplateWxRedisOps(), SendAccountConstant.MINI_PROGRAM_TOKEN_PREFIX);
        config.setAppid(miniProgramAccount.getAppId());
        config.setSecret(miniProgramAccount.getAppSecret());
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }


}
